// 在线游戏功能测试
export default class OnlineGameTest {
  constructor() {
    this.openid = null
    this.roomId = null
  }

  // 运行完整测试
  async runFullTest() {
    try {
      console.log('=== 开始在线游戏功能测试 ===')
      
      // 1. 获取OpenID
      await this.getOpenId()
      
      // 2. 创建房间
      const roomId = await this.createRoom()
      console.log('房间创建成功:', roomId)
      
      // 3. 模拟第二个玩家加入
      await this.simulateSecondPlayerJoin(roomId)
      
      // 4. 检查房间状态
      await this.checkRoomStatus(roomId)
      
      // 5. 测试落子功能
      await this.testPlacePiece(roomId)
      
      // 6. 测试认输功能
      await this.testSurrender(roomId)
      
      console.log('=== 在线游戏功能测试完成 ===')
    } catch (error) {
      console.error('测试失败:', error)
    }
  }

  // 获取OpenID
  async getOpenId() {
    try {
      const { result } = await wx.cloud.callFunction({
        name: 'getOpenId'
      })
      this.openid = result.openid
      console.log('OpenID获取成功:', this.openid)
    } catch (error) {
      console.error('获取OpenID失败:', error)
      throw error
    }
  }

  // 创建房间
  async createRoom() {
    const roomId = 'test_online_' + Date.now()
    try {
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: 'create_room',
          roomId: roomId,
          playerName: '测试创建者',
          openid: this.openid
        }
      })

      if (result.success) {
        this.roomId = roomId
        return roomId
      } else {
        throw new Error(result.message)
      }
    } catch (error) {
      console.error('创建房间失败:', error)
      throw error
    }
  }

  // 模拟第二个玩家加入
  async simulateSecondPlayerJoin(roomId) {
    try {
      const secondPlayerOpenid = 'second_player_' + Date.now()
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: 'join_room',
          roomId: roomId,
          playerName: '测试加入者',
          openid: secondPlayerOpenid
        }
      })

      if (result.success) {
        console.log('第二个玩家加入成功')
      } else {
        throw new Error(result.message)
      }
    } catch (error) {
      console.error('第二个玩家加入失败:', error)
      throw error
    }
  }

  // 检查房间状态
  async checkRoomStatus(roomId) {
    try {
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: 'get_room_info',
          roomId: roomId
        }
      })

      if (result.success) {
        const roomData = result.data
        console.log('房间状态检查:', {
          status: roomData.status,
          currentPlayer: roomData.currentPlayer,
          player1: roomData.player1,
          player2: roomData.player2,
          myOpenid: this.openid
        })
        
        // 检查玩家颜色分配
        const isCreator = roomData.player1 === this.openid
        const expectedColor = isCreator ? 1 : 2
        console.log('玩家颜色检查:', {
          isCreator: isCreator,
          expectedColor: expectedColor,
          myOpenid: this.openid
        })
      }
    } catch (error) {
      console.error('检查房间状态失败:', error)
    }
  }

  // 测试落子功能
  async testPlacePiece(roomId) {
    try {
      console.log('测试落子: (7, 7)')
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: 'place_piece',
          roomId: roomId,
          x: 7,
          y: 7,
          openid: this.openid
        }
      })

      console.log('落子结果:', result)

      if (result.success) {
        console.log('✅ 落子成功')
      } else {
        console.log('❌ 落子失败:', result.message)
      }
    } catch (error) {
      console.error('落子测试失败:', error)
    }
  }

  // 测试认输功能
  async testSurrender(roomId) {
    try {
      console.log('测试认输功能')
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: 'surrender',
          roomId: roomId,
          openid: this.openid
        }
      })

      console.log('认输结果:', result)

      if (result.success) {
        console.log('✅ 认输成功')
      } else {
        console.log('❌ 认输失败:', result.message)
      }
    } catch (error) {
      console.error('认输测试失败:', error)
    }
  }

  // 测试类型一致性
  async testTypeConsistency(roomId) {
    try {
      console.log('测试类型一致性...')
      
      // 获取房间信息
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: 'get_room_info',
          roomId: roomId
        }
      })

      if (result.success) {
        const roomData = result.data
        const currentPlayer = String(roomData.currentPlayer)
        const myOpenid = String(this.openid)
        
        console.log('类型一致性检查:', {
          currentPlayer: currentPlayer,
          myOpenid: myOpenid,
          isMatch: currentPlayer === myOpenid,
          currentPlayerType: typeof roomData.currentPlayer,
          myOpenidType: typeof this.openid
        })
      }
    } catch (error) {
      console.error('类型一致性测试失败:', error)
    }
  }
}
